var signApply = {
    init: function() {
        var that = this;
        that.getAddress(10, '', $("select[data-name=Aprovince]"));
        that.getAddress(20, '', $("select[data-name=Acity]"));
        that.getAddress(30, '', $("select[data-name=Atown]"));
        $.ajaxSend('api/CompanyApply/GetList?token=' + $.cookie('token'), {
            "page": 1,
            "pagesize": 20,
            "companyname": '',
            "cityid": ''
        }, function(redata) {
        		if(redata.recode=='1003'){
                window.location.href = '/nlogin.html';
                return;}
            if (redata.recode == '0000' && redata.mod!=null) {
               
                var compiled = _.template($("#signAppyTpl").html());
                $("#signApplyCon").html(compiled(redata.mod));

                $("#signApplyPage").show().jqPaginator({
                    //totalPages: Math.ceil(redata.mod.TotalNum/20),
                    totalPages: redata.mod.TotalPageCount?redata.mod.TotalPageCount:1,
                    visiblePages: 5,
                    currentPage: 1,
                    first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
                    prev: '<li class="prev"><a href="javascript:void(0);"><i class="arrow arrow2"><\/i>上一页<\/a><\/li>',
                    next: '<li class="next"><a href="javascript:void(0);">下一页<i class="arrow arrow3"><\/i><\/a><\/li>',
                    last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
                    page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
                    onPageChange: function(num, type) {
                        $("#signApplyPage").attr('curpage', num);
                      $("#shpage").val(num);
                        if(type =='change'){
                            that.doPage();
                        }
                    }
                });


                $("#signApplyCon").on("click", '.jApplyPass', function(){
                    var wuliu_id = $(this).attr('wuliu_id'),
                        wuliu_no = $(this).attr('wuliu_no'),
                        wuliu_name = $(this).attr('wuliu_name');
                     $.ajax({
                            url: COMMONURL + 'api/CompanyApply/Apply?token=' + $.cookie('token'),
                            type: 'post',
                            dataType: 'json',
                            data: {
                                wuliu_id:wuliu_id,
                                wuliu_no: wuliu_no,
                                wuliu_name: wuliu_name
                            }

                        }).done(function(data){
                            if(redata.recode == '0000'){
                            alert('操作成功');
                            window.location.reload();
                          }
                        })
                })


            }else{
                var compiled = _.template($("#signAppyTpl").html());
                $("#signApplyCon").html(compiled(redata.mod));
            }
        });

        $("#signApplyQuery").click(function() {
            that.doSearch();
        });
    },
    getAddress: function(type, sub, obj) {
        $.ajax({
            url: COMMONURL + 'api/CityBase/GetCityBaseList?token=' + $.cookie('token'),
            type: 'post',
            dataType: 'json',
            data: {
                type: type,
                sub_city_id: sub
            }

        }).done(function(data) {
            var arry = [];
            if (!!data.mod) {
                if (!data.mod.length) {
                    arry.push('<option value="' + '" data-id="' + '">' + '请选择' + '</option>');
                } else {
                    $.each(data.mod, function(i, v) {
                        arry.push('<option value="' + v.city_name + '" data-id="' + v.city_id + '">' + v.city_name + '</option>');
                    });
                }
                obj.html(arry.join(''));
            }

        })
    },
    doPage:function(){
        var page = $.trim($("#shpage").val());
        $.ajax({
            url: COMMONURL + "api/CompanyApply/GetList?token=" + $.cookie('token'),
            type: "post",
            data: {
                page: page,
                pagesize: 20,
                companyname: $("#CompanyInp").val(),
                cityid: '',
            },
            dataType: "json"
        }).done(function(redata) {
            if (redata.recode == '0000' && redata.mod!=null) {
                var compiled = _.template($("#signAppyTpl").html());
                $("#signApplyCon").html(compiled(redata.mod));
            }else{
                var compiled = _.template($("#signAppyTpl").html());
               
                $("#signApplyCon").html(compiled(redata.mod));
                $("#signApplyPage").hide();
            }
        });
    },
    doSearch: function() {
        var page = $.trim($("#shpage").val());
        $.ajax({
            url: COMMONURL + "api/CompanyApply/GetList?token=" + $.cookie('token'),
            type: "post",
            data: {
                page: page,
                pagesize: 20,
                companyname: $("#CompanyInp").val(),
                cityid: '',
            },
            dataType: "json"
        }).done(function(redata) {
            if (redata.recode == '0000' && redata.mod!=null) {
                var compiled = _.template($("#signAppyTpl").html());
                $("#signApplyCon").html(compiled(redata.mod));
                $("#signApplyPage").show().jqPaginator({
                    //totalPages: Math.ceil(redata.mod.TotalNum/20),
                    totalPages: redata.mod.TotalPageCount?redata.mod.TotalPageCount:1,
                    visiblePages: 5,
                    currentPage: 1,
                    first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
                    prev: '<li class="prev"><a href="javascript:void(0);"><i class="arrow arrow2"><\/i>上一页<\/a><\/li>',
                    next: '<li class="next"><a href="javascript:void(0);">下一页<i class="arrow arrow3"><\/i><\/a><\/li>',
                    last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
                    page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
                    onPageChange: function(num, type) {
                        $("#signApplyPage").attr('curpage', num);
                      $("#shpage").val(num);
                        if(type =='change'){
                            that.doPage();
                        }
                    }
                });
            }else{
                var compiled = _.template($("#signAppyTpl").html());
               
                $("#signApplyCon").html(compiled(redata.mod));
                $("#signApplyPage").hide();
            }
        });

    }
}


signApply.init();
